/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.catosist.run.application.Util;

import java.awt.Color;
import java.awt.Component;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import javax.swing.JTable;
import javax.swing.table.DefaultTableCellRenderer;

/**
 *
 * @author igorov
 */
public class ColoredTableCellRenderer extends DefaultTableCellRenderer {

    final long MILLSECS_PER_DAY = 86400000; //Milisegundos al día

    public Component getTableCellRendererComponent(JTable table, Object value,
            boolean selected, boolean focused, int row, int column) {


        try {

            setEnabled(table == null || table.isEnabled()); // see question above
            DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
            Date fechaInicial = df.parse((String) table.getValueAt(row, 2));

            String posibleFechaCancelacion = (String) table.getValueAt(row, 9);

            //Si no hay fecha de cancelacion
            if (posibleFechaCancelacion.compareTo("No") == 0) {
                Date fechaActual = new Date();

                long diferencia = (fechaActual.getTime() - fechaInicial.getTime()) / MILLSECS_PER_DAY;
                if (diferencia > 31) {
                    setBackground(Color.red);
                } else if (diferencia > 21) {
                    setBackground(Color.orange);
                } else {
                    setBackground(Color.yellow);
                }

            } else {
                setBackground(Color.green);
            }

            super.getTableCellRendererComponent(table, value, selected, focused,
                    row, column);

        } catch (Exception e) {
        }
        return this;
    }
}
